//
//  ArticleViewCell.m
//  time
//
//  Created by bingo on 2018/5/20.
//  Copyright © 2018年 1ge. All rights reserved.
//

#import "ArticleViewCell.h"

#import "SchoolCollectionViewCell.h"
#import "CommonModel.h"
#import <Masonry/Masonry.h>
#import "MLAWebVC.h"

static NSString *kSchoolCollectionViewCell = @"SchoolCollectionViewCell.h";

@interface ArticleViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ArticleViewCell
#pragma mark-
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        [self setupLayout];
    }
    return self;
}

- (void)setDataSource:(NSMutableArray<CommonModel *> *)dataSource {
    
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (void)setupUI {
    UICollectionViewFlowLayout *collectionLayout = [UICollectionViewFlowLayout new];
    collectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    collectionLayout.minimumInteritemSpacing = 5;//横向item间距(最小值)
    collectionLayout.minimumLineSpacing = 5;//行间距
    
    collectionLayout.itemSize = CGSizeMake(146, 130);//每个UICollectionViewCell 的大小
    collectionLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);//每个UICollectionViewCell 的 margin
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionLayout];
    self.collectionView.collectionViewLayout = collectionLayout;
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.clipsToBounds = YES;
    
    [self.collectionView registerClass:[SchoolCollectionViewCell class] forCellWithReuseIdentifier:kSchoolCollectionViewCell];
    
}
- (void)setupLayout {
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SchoolCollectionViewCell *cell = (SchoolCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kSchoolCollectionViewCell forIndexPath:indexPath];
    CommonModel *model = [self.dataSource objectAtIndex:indexPath.row];
    
    if ([model.imageUrl containsString:@"http:"]) {
        [cell.itemImageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];
    } else {
        [cell.itemImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASE_IMAGE_URL,model.imageUrl]]];
    }
    
    cell.titleLabel.text = model.title;
    cell.descripLabel.text = model.source;
    cell.timeLabel.text = [[model.addDate componentsSeparatedByString:@" "] objectAtIndex:0];
    return cell;
}

#pragma mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CommonModel *model = _dataSource[indexPath.row];
    MLAWebVC *webVC = [[MLAWebVC alloc] init];
    webVC.titleStr = model.title;
    webVC.urlStr = model.navigationUrl;
    [self.viewController.navigationController pushViewController:webVC animated:YES];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
